| Conditions | 2 |
| Paths | 8 |
| Total Lines | 99 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | ////////////////////////////////////////////////////////////////////////////////////// |
||
| 172 | function draw(str, to, first, box) { |
||
| 173 | tag || init(); |
||
| 174 | |||
| 175 | var re = /("(?:((?:https?|file):\/\/(?:\\?\S)+?)|(?:\\?.)*?)")\s*(:?)|-?\d+\.?\d*(?:e[+-]?\d+)?|true|false|null|[[\]{},]|(\S[^-[\]{},"\d]*)/gi |
||
| 176 | , node = div.cloneNode() |
||
| 177 | , link = document.createElement("a") |
||
| 178 | , span = document.createElement("span") |
||
| 179 | , info = document.createElement("i") |
||
| 180 | , colon = document.createTextNode(": ") |
||
| 181 | , comma = fragment(",") |
||
| 182 | , path = [] |
||
| 183 | , cache = { |
||
| 184 | "{": fragment("{", "}"), |
||
| 185 | "[": fragment("[", "]") |
||
| 186 | }; |
||
| 187 | |||
| 188 | node.className = "R" + rand + (box ? " " + box : ""); |
||
| 189 | |||
| 190 | link.classList.add("L" + rand); |
||
| 191 | info.classList.add("I" + rand); |
||
| 192 | |||
| 193 | to.addEventListener("click", function(e) { |
||
| 194 | var target = e.target, open = target.classList.contains(COLL); |
||
| 195 | if (target.tagName == "I") { |
||
| 196 | if (e.altKey) { |
||
| 197 | changeSiblings(target, COLL, !open); |
||
| 198 | } else if (e[mod]) { |
||
| 199 | open = target.nextSibling.querySelector("i"); |
||
| 200 | if (open) change(target.nextSibling, "i", COLL, !open.classList.contains(COLL)); |
||
| 201 | } else { |
||
| 202 | target.classList[open ? "remove" : "add"](COLL); |
||
| 203 | } |
||
| 204 | } |
||
| 205 | }, true); |
||
| 206 | |||
| 207 | to.replaceChild(box = node, first); |
||
| 208 | loop(str, re); |
||
| 209 | |||
| 210 | function loop(str, re) { |
||
| 211 | str = reconvert(str); |
||
| 212 | var match, val, tmp, i = 0, len = str.length; |
||
| 213 | try { |
||
| 214 | for (; match = re.exec(str); ) { |
||
| 215 | val = match[0]; |
||
| 216 | if (val == "{" || val == "[") { |
||
| 217 | path.push(node); |
||
| 218 | node.appendChild(cache[val].cloneNode(true)); |
||
| 219 | node = node.lastChild.previousSibling; |
||
| 220 | node.len = 1; |
||
| 221 | node.start = re.lastIndex; |
||
| 222 | } else if ((val == "}" || val == "]") && node.len) { |
||
| 223 | if (node.childNodes.length) { |
||
| 224 | tmp = info.cloneNode(); |
||
| 225 | tmp.dataset.content = node.len + ( |
||
| 226 | node.len == 1 ? |
||
| 227 | (val == "]" ? " item, " : " property, ") : |
||
| 228 | (val == "]" ? " items, " : " properties, ") |
||
| 229 | ) + units(re.lastIndex - node.start + 1); |
||
| 230 | |||
| 231 | if ((val = node.previousElementSibling) && val.className == KEY) { |
||
| 232 | tmp.dataset.key = reconvert(val.textContent.slice(1, -1).replace(/'/, "\\'")); |
||
| 233 | } |
||
| 234 | node.parentNode.insertBefore(tmp, node); |
||
| 235 | } else { |
||
| 236 | node.parentNode.removeChild(node); |
||
| 237 | } |
||
| 238 | node = path.pop(); |
||
| 239 | } else if (val == ",") { |
||
| 240 | node.len += 1; |
||
| 241 | node.appendChild(comma.cloneNode(true)); |
||
| 242 | } else { |
||
| 243 | if (match[2]) { |
||
| 244 | tmp = link.cloneNode(); |
||
| 245 | tmp.href = match[2].replace(/\\"/g, '"'); |
||
| 246 | } else { |
||
| 247 | tmp = span.cloneNode(); |
||
| 248 | } |
||
| 249 | tmp.textContent = match[1] || val; |
||
| 250 | tmp.classList.add(match[3] ? KEY : match[1] ? STR : match[4] ? ERR : BOOL); |
||
| 251 | node.appendChild(tmp); |
||
| 252 | if (match[3]) { |
||
| 253 | node.appendChild(colon.cloneNode()); |
||
| 254 | } |
||
| 255 | } |
||
| 256 | if (++i > 1000) { |
||
| 257 | document.title = (0|(100*re.lastIndex/len)) + "% of " + units(len); |
||
| 258 | return setTimeout(function() { loop(str, re) }); |
||
| 259 | } |
||
| 260 | } |
||
| 261 | document.title = "" |
||
| 262 | JSON.parse(str) |
||
| 263 | } catch(e) { |
||
| 264 | tmp = document.createElement("h3"); |
||
| 265 | tmp.className = ERR; |
||
| 266 | tmp.textContent = e; |
||
| 267 | box.insertBefore(tmp, box.firstChild); |
||
| 268 | } |
||
| 269 | } |
||
| 270 | } |
||
| 271 | |||
| 312 |